#include "theaterpage.h"
#include "control/votedialog.h"
#include "director.h"
#include "ui_theaterpage.h"
#include <QDebug>
#include <QDir>
#include <QJsonObject>
TheaterPage::TheaterPage(QWidget *parent)
    : AbstractPage(parent), ui(new Ui::TheaterPage) {
  ui->setupUi(this);
  layout = new QVBoxLayout(ui->widget_2);
  ui->widget_2->setStyleSheet("#userheader{"
                              "border: 1px solid drak; "
                              "background-color: white;"
                              "border-style: outset;"
                              "border-radius: 5px;"
                              "padding: 6px;"
                              "}#userheader QLabel { color: black; }");
  QStringList lists = QDir(":/profile/").entryList({"*.png"});
  for (QString &image : lists) {
    m_files.append(QString(":/profile/%1").arg(image));
  }
  qDebug() << "alll header picture" << m_files;
}

TheaterPage::~TheaterPage() { delete ui; }

void TheaterPage::updated() {
  // 显示所有用户
  for (User *user : Director::instance()->users) {
    layout->addWidget(createUserWidget(user));
  }
  ui->peopleCount->setText(
      QString("%1").arg(Director::instance()->users.size()));
  ui->wordLabel->setText(Director::instance()->word);
}

void TheaterPage::leave() {
  for (int i = layout->count() - 1; i >= 0; i--)
    layout->removeItem(layout->itemAt(i));
}

void TheaterPage::notify(const QJsonObject &obj) {
  QString cmd = obj.value("event").toString();
  QString msg = obj.value("msg").toString();
  if (cmd == "talk") {
    // 发送描述词的信息
    showDescriptionWordDlg();
  } else if (cmd == "say") {
    // 接收服务器发来的描述词的信息
    ui->textBrowser->append(msg);
  } else if (cmd == "ticket") {
    // 投票信息
    for (User *u : Director::instance()->users) {
      if (u->nick == Director::instance()->nick) {
        if (u->out)
          return;
      }
    }
    VoteDialog vDlg;
    vDlg.exec();
  } else if (cmd == "result") {
    // 游戏最后的结果
    for (User *u : Director::instance()->users) {
      if (u->nick != Director::instance()->nick)
        continue;
      Director::instance()->m_result =
          !u->word.trimmed().compare(msg.trimmed());
      break;
    }

    Director::instance()->backward();
  }
}

// leave
void TheaterPage::on_pushButton_clicked() { Director::instance()->forward(); }

QWidget *TheaterPage::createUserWidget(User *user) {
  QWidget *userWidget = new QFrame(this);
  userWidget->setObjectName("userheader");
  QLabel *profile = new QLabel(userWidget);
  profile->setPixmap(QPixmap(m_files.at(std::rand() % m_files.size())));
  profile->setScaledContents(true);
  profile->setFixedSize(50, 50);

  QLabel *label = new QLabel(userWidget);
  QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
  sizePolicy.setHorizontalStretch(0);
  sizePolicy.setVerticalStretch(0);
  sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
  label->setSizePolicy(sizePolicy);
  label->setAlignment(Qt::AlignCenter);
  label->setText(user->nick);
  label->setMaximumWidth(75);

  QBoxLayout *userLayout = new QHBoxLayout(userWidget);
  userLayout->addWidget(profile);
  userLayout->addWidget(label);
  userLayout->setMargin(0);
  return userWidget;
}

void TheaterPage::showDescriptionWordDlg() {
  QDialog dialog;
  Qt::WindowFlags flages = dialog.windowFlags();
  dialog.setWindowFlags(
      flages & ~(Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint));
  dialog.setObjectName("expressionDlg");
  dialog.setAutoFillBackground(true);
  dialog.setStyleSheet(
      "#expressionDlg{border-image: "
      "url(:/imgs/backgroun.jpg);}MainDialog QLabel{color : white;}");
  dialog.setWindowFlags(Qt::WindowCloseButtonHint);
  QLabel *label =
      new QLabel(QString("%1 :").arg(Director::instance()->nick), &dialog);
  QTextEdit *textEdit = new QTextEdit(&dialog);
  QPushButton *button = new QPushButton("发送", &dialog);
  button->setEnabled(false);
  connect(textEdit, &QTextEdit::textChanged, this, [&button, textEdit] {
    button->setEnabled(textEdit->toPlainText().size() > 0);
  });
  connect(button, &QPushButton::clicked, this, [textEdit, &dialog] {
    Director::instance()->sendTalk(textEdit->toPlainText());
    dialog.accept();
  });
  QVBoxLayout *layout = new QVBoxLayout(&dialog);
  layout->addWidget(label);
  layout->addWidget(textEdit);
  layout->addWidget(button);
  dialog.exec();
}
